var jsonMyRequestData = {
    search_value: "",
    "column-sort": "",
    "column-sort-type": "",  
}
var columnSort = [];
$(document).ready(function(){
    //My Request
    $("body").on("keyup","#txtSearchMyRequestDatatable",function(e){
        $("#loadMyRequestTable").dataTable().fnDestroy();
        jsonMyRequestData['search_value'] = $(this).val();
        MyRequestDataTable.defaultData();
    })
	MyRequestDataTable.defaultData();
    $("body").on("click","#loadMyRequestTable th",function(){
        var cursor = $(this).css("cursor");
        jsonMyRequestData['column-sort'] = $(this).attr("field_name");
        if($(this).attr("field_name")=="" || $(this).attr("field_name")==undefined){
            return;
        }
        var indexcolumnSort = columnSort.indexOf($(this).attr("field_name"));

        $(this).closest("#loadMyRequestTable").find(".sortable-image").html("");
        if(indexcolumnSort==-1){
            columnSort.push($(this).attr("field_name"))
            $(this).find(".sortable-image").html("<i class='fa fa-sort-asc'></i>")
            jsonMyRequestData['column-sort-type'] = "ASC";
        }else{
            columnSort.splice(indexcolumnSort,1);
            $(this).find(".sortable-image").html("<i class='fa fa-sort-desc'></i>")
            jsonMyRequestData['column-sort-type'] = "DESC";
        }
        
        $("#loadMyRequestTable").dataTable().fnDestroy();
        MyRequestDataTable.defaultData();
    })
    $("body").on("click","#loadMyRequestTable tbody tr td",function(e){
       var href = $(this).closest("tr").find(".linkTrigger").attr("href");
       if(href=="" || href==null || href=="undefined" || href==undefined){
        return;
       }
       window.location = href;
       // alert(123)
    })
})
MyRequestDataTable = {
    "defaultData" : function(){
        // var oTable = $('#loadMyRequestTable').dataTable( {
        //     "oLanguage": {
        //         "sProcessing": '<div class="spinner load_m"  style="margin-top:50px;"> '+
        //                '<div class="bar1"></div> '+
        //                '<div class="bar2"></div> '+
        //                '<div class="bar3"></div> '+
        //                '<div class="bar4"></div> '+
        //                '<div class="bar5"></div> '+
        //                '<div class="bar6"></div> '+
        //               '<div class="bar7"></div> '+
        //               '<div class="bar8"></div> '+
        //               '<div class="bar9"></div> '+
        //               '<div class="bar10"></div> '+
        //          '</div>'
        //     },
        //             "bLengthChange": false,
        //             "bFilter": false,
        //              "bProcessing": true,
        //              "bRetrieve": true,
        //              "aaSorting" : [[2,"desc"]],
        //              //"bPaginate": true,//"sEcho":3,//"iTotalDisplayRecords":10,//"iTotalRecords":"10",
        //              //"iTotalDisplayRecords":"10",//"bServerSide": true,
        //              "sAjaxSource": "/ajax/my_request",
        //              "sPaginationType": "full_numbers",
        //              "sAjaxDataProp": "",
        //              "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        //              "aoColumns": [
        //                  { "mData": "form_name" },
        //                  { "mData": function(source, type, val){
        //                         return '<a href="/user_view/workspace?view_type=request&formID='+ source['form_id'] +'&requestID='+ source['request_id'] +'" title="" data-original-title="">'+ source['TrackNo'] +'</a>';
        //                      } 
        //                  },
        //                  { "mData": "form_status" },
        //                  { "mData": "DateCreated" }
        //              ],
        //              "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
        //                  $("#example_length > label > select").addClass("selectlength");
        //              },
        //              "aoColumnDefs": [ {
        //                  "bSortable": true, "aTargets": [ 0,1,2,3 ]
        //                  },{
        //                      "bSortable": false, "aTargets": [ "_all" ]
        //                  }/*, {
        //                      "bSearchable": true, "aTargets": [ 0 ]
        //                  }, {
        //                      "bSearchable": false, "aTargets": [ "_all" ]
        //              }*/
        //              ],
        //              "fnServerData": function (sSource, aoData, fnCallback) {
        //                  $.ajax({
        //                      type: "POST",
        //                      cache: false,
        //                      dataType: 'json',
        //                      url: sSource,
        //                      data: {action:"loadData"},
        //                      success: function (json) {
        //                          // Retrieve data
        //                          // console.log(json)
        //                          // return;
        //                          fnCallback(json);
        //                      }
        //                  });
        //              }
        //          });
        //         return oTable;
        var oTable = $('#loadMyRequestTable').dataTable( {
            "sDom": 'Rlfrtip',
            // "sScrollY": "400px",
            "oLanguage": {
                "sProcessing": '<div class="spinner load_m"> '+
                       '<div class="bar1"></div> '+
                       '<div class="bar2"></div> '+
                       '<div class="bar3"></div> '+
                       '<div class="bar4"></div> '+
                       '<div class="bar5"></div> '+
                       '<div class="bar6"></div> '+
                      '<div class="bar7"></div> '+
                      '<div class="bar8"></div> '+
                      '<div class="bar9"></div> '+
                      '<div class="bar10"></div> '+
                 '</div>'
            },
            "oColReorder": false,
            "bLengthChange": false,
            "bFilter": false,
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "bSort": false,
            "sAjaxSource": "/ajax/my_request",
            "fnServerData": function(sSource, aoData, fnCallback) {
                aoData.push({"name": "search_value", "value": jsonMyRequestData['search_value']});
                aoData.push({"name": "action", "value": "loadDataDatatable"});
                aoData.push({"name": "column-sort", "value": jsonMyRequestData['column-sort']});
                aoData.push({"name": "column-sort-type", "value": jsonMyRequestData['column-sort-type']});
                
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": function(data) {
                        console.log(data)
                        // return;
                        fnCallback(data);
                    }
                });
            },
            "fnInitComplete": function(oSettings, json) {
                // 
            }
        });
        return oTable;
    }
}